<?php

class ShoppingCartAction extends CommonAction{
	public function index(){
		$this -> assign("shopping_cart", $_SESSION[self::SHOPPING_CART]);
		
		$this->assign("allCategories",$this->getAllCategories());
		
		$this->display();
	}

	public function add(){
		for ($i = 0;$i < count($SizeList); $i++) {
			$productSpecs[] = array('size'=> $SizeList[$i]);
		}
		$product = D("Product")->find($_POST["product_id"]);
		$product["category_id"] = $_POST["category_id"];
		$product["size"] = $_POST["size"];
		
		$_SESSION[self::SHOPPING_CART][] = $product;
		$this->ajaxReturn($_POST['id'], 'Add to shoppingcart successfull!',1);
	}

	public function buy(){
		$product = D("Product")->find($_POST["product_id"]);
		
		$product["size"] = $_POST["size"];
		$product["qty"] = 1;

		$_SESSION[self::SHOPPING_CART][] = $product;
		
		$this->redirect("ShoppingCart/index");
	}

	public function delete(){
		for($i=0; $i<count($_SESSION[self::SHOPPING_CART]);$i++){
				if($_SESSION[self::SHOPPING_CART][$i]["id"] == $_POST["product_id"]
					&& $_SESSION[self::SHOPPING_CART][$i]["category_id"] == $_POST["category_id"]
					&& $_SESSION[self::SHOPPING_CART][$i]["size"] == $_POST["size"]){
					array_splice($_SESSION[self::SHOPPING_CART], $i, 1);
					break;
				}
		}
		$this->ajaxReturn($_POST['id'], 'Delete from shoppingcart successfull!',1);
	}

	public function clear(){
		$_SESSION[self::SHOPPING_CART] = null;
		$this->redirect("ShoppingCart/index");
	}
}

?>